package com.zmc.lostfound.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.zmc.lostfound.auth.service.UserService;
import com.zmc.lostfound.auth.vo.*;
import com.zmc.lostfound.common.dto.RoleAndPermissionDTO;
import com.zmc.lostfound.common.dto.post.FromUser;
import com.zmc.lostfound.common.utils.PageParams;
import com.zmc.lostfound.common.utils.PageResult;
import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.common.valid.AddGroup;
import com.zmc.lostfound.common.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZMC
 * @description 用户信息管理接口
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:09:50
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户信息管理接口", tags = "用户信息管理接口")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("分页查询用户基本信息")
    @PostMapping("/query/page")
    public Result queryPage(@RequestBody @Validated PageParams pageParams) {
        PageResult pageResult = userService.queryPage(pageParams);
        return Result.ok("查询成功").put("pageResult", pageResult);
    }

    @ApiOperation("按用户ids批量查询用户基本信息")
    @PostMapping("/query/baseInfoByIds")
    public Result queryBaseInfoByIds(@RequestBody List<Long> userIds) {
        List<UserBaseInfoVO> userBaseInfoVOList = userService.queryBaseInfoByIds(userIds);
        return Result.ok("查询成功").put("userBaseInfoVOList", userBaseInfoVOList);
    }

    @ApiOperation("按用户ids批量查询用户部分信息")
    @PostMapping("/query/partInfoByIds")
    public Result queryPartInfoByIds(@RequestBody List<Long> userIds) {
        List<FromUser> fromUserList = userService.queryPartInfoByIds(userIds);
        return Result.ok("查询成功").put("fromUserList", fromUserList);
    }

    @ApiOperation("按id查询用户详情信息")
    @GetMapping("/query/detailInfoById/{id}")
    public Result queryDetailInfoById(@PathVariable("id") Long id) {
        UserDetailInfoVO userDetailInfoVO = userService.queryDetailInfoById(id);
        return Result.ok("查询成功").put("userDetailInfoVO", userDetailInfoVO);
    }

    @ApiOperation("按id查询用户基本信息")
    @GetMapping("/query/baseInfoById/{id}")
    public Result queryBaseInfoById(@PathVariable("id") Long id) {
        UserBaseInfoVO userBaseInfoVO = userService.queryBaseInfoById(id);
        return Result.ok("查询成功").put("userBaseInfoVO", userBaseInfoVO);
    }

    @ApiOperation("按id查询用户的角色与权限信息")
    @GetMapping("/query/roleAndPermission/{id}")
    public Result queryRoleAndPermissionById(@PathVariable("id") Long id) {
        RoleAndPermissionDTO roleAndPermissionDTO = userService.queryRoleAndPermissionById(id);
        return Result.ok("查询成功").put("roleAndPermissionDTO", roleAndPermissionDTO);
    }

    @ApiOperation("按手机号查询用户详情信息")
    @GetMapping("/query/detailInfoByPhone/{phone}")
    public Result queryDetailInfoByPhone(@PathVariable("phone") String phone) {
        UserDetailInfoVO userDetailInfoVO = userService.queryDetailInfoByPhone(phone);
        return Result.ok("查询成功").put("userDetailInfoVO", userDetailInfoVO);
    }

    @ApiOperation("按角色id查询用户基本信息")
    @GetMapping("/query/baseInfoByRoleId/{roleId}")
    public Result queryBaseInfoByRoleId(@PathVariable("roleId") Long roleId) {
        List<UserBaseInfoVO> userBaseInfoVOList = userService.queryBaseInfoByRoleId(roleId);
        return Result.ok("查询成功").put("userBaseInfoVOList", userBaseInfoVOList);
    }

    @ApiOperation("管理员新增用户基本信息")
    @PostMapping("/insertUserWithAdmin")
    public Result insertUserWithAdmin(@RequestBody @Validated(value = {AddGroup.class}) UserSaveWithAdminVO userSaveWithAdminVO) {
        return userService.insertUserWithAdmin(userSaveWithAdminVO);
    }

    @ApiOperation("管理员修改用户基本信息")
    @PutMapping("/updateUserWithAdmin")
    public Result updateUserWithAdmin(@RequestBody @Validated(value = {UpdateGroup.class}) UserSaveWithAdminVO userSaveWithAdminVO) {
        return userService.updateUserWithAdmin(userSaveWithAdminVO);
    }

    @ApiOperation("用户修改基本信息")
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody @Validated UserUpdateVO userUpdateVO) {
        return userService.updateUser(userUpdateVO);
    }

    @ApiOperation("发送短信验证码")
    @GetMapping("/sendSmsCaptcha")
    public Result sendSmsCaptcha(@RequestParam("phone") String phone) {
        return userService.sendSmsCaptcha(phone);
    }

    @ApiOperation("校验手机号验证码是否正确")
    @GetMapping("/checkPhoneCaptcha")
    public Result checkPhoneCaptcha(@RequestParam("phone") String phone, @RequestParam("captcha") String captcha) {
        return userService.checkPhoneCaptcha(phone, captcha);
    }

    @ApiOperation("用户更换绑定手机号")
    @PutMapping("/updatePhone")
    public Result updatePhone(@RequestBody @Validated UpdatePhoneVO updatePhoneVO) {
        return userService.updatePhone(updatePhoneVO);
    }

    @ApiOperation("通过短信验证码修改密码")
    @PutMapping("/updatePasswordByCaptcha")
    public Result updatePasswordByCaptcha(@RequestBody @Validated UpdatePasswordVO updatePasswordVO) {
        return userService.updatePasswordByCaptcha(updatePasswordVO);
    }

    @ApiOperation("发送邮箱验证码")
    @GetMapping("/sendEmailCaptcha")
    public Result sendEmailCaptcha(@RequestParam("qqEmail") String qqEmail) {
        return userService.sendEmailCaptcha(qqEmail);
    }

    @ApiOperation("校验邮箱验证码是否正确")
    @GetMapping("/checkEmailCaptcha")
    public Result checkEmailCaptcha(@RequestParam("qqEmail") String qqEmail, @RequestParam("captcha") String captcha) {
        return userService.checkEmailCaptcha(qqEmail, captcha);
    }

    @ApiOperation("用户更换绑定邮箱")
    @PutMapping("/updateEmail")
    public Result updateEmail(@RequestBody @Validated UpdateEmailVO updateEmailVO) {
        return userService.updateEmail(updateEmailVO);
    }

    @ApiOperation("短信验证码注册")
    @PostMapping("/phoneCaptchaRegister")
    public Result phoneCaptchaRegister(@RequestBody @Validated PhoneCaptchaRegisterVO phoneCaptchaRegisterVO) {
        return userService.phoneCaptchaRegister(phoneCaptchaRegisterVO);
    }

    @ApiOperation("短信验证码登录")
    @PostMapping("/phoneCaptchaLogin")
    public Result phoneCaptchaLogin(@RequestBody @Validated PhoneCaptchaLoginVO phoneCaptchaLoginVO) {
        return userService.phoneCaptchaLogin(phoneCaptchaLoginVO);
    }

    @ApiOperation("手机号密码登录")
    @PostMapping("/phonePasswordLogin")
    public Result phonePasswordLogin(@RequestBody @Validated PhonePasswordLoginVO phonePasswordLoginVO) {
        return userService.phonePasswordLogin(phonePasswordLoginVO);
    }

    @ApiOperation("查询当前登录用户个人基本信息")
    @GetMapping("/query/mySelfBaseInfo")
    public Result queryMySelfBaseInfo() {
        UserBaseInfoVO userBaseInfoVO = userService.queryBaseInfoById(StpUtil.getLoginIdAsLong());
        return Result.ok("查询成功").put("userBaseInfoVO", userBaseInfoVO);
    }

    @ApiOperation("查询当前登录用户的角色与权限信息")
    @GetMapping("/query/MySelfRoleAndPermission")
    public Result queryMySelfRoleAndPermission() {
        RoleAndPermissionDTO roleAndPermissionDTO = userService.queryRoleAndPermissionById(StpUtil.getLoginIdAsLong());
        return Result.ok("查询成功").put("roleAndPermissionDTO", roleAndPermissionDTO);
    }

    @ApiOperation("查询当前登录用户个人详细信息")
    @GetMapping("/query/mySelfDetailInfo")
    public Result queryMySelfDetailInfo() {
        UserDetailInfoVO userDetailInfoVO = userService.queryDetailInfoById(StpUtil.getLoginIdAsLong());
        return Result.ok("查询成功").put("userDetailInfoVO", userDetailInfoVO);
    }

    @ApiOperation("查询当前用户登录状态")
    @GetMapping("/isLogin")
    public Result isLogin() {
        return Result.ok().put("isLogin", StpUtil.isLogin());
    }

    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public Result logout() {
        // 本质是删除 当前登录用户 存储在redis的token信息
        StpUtil.logout();
        return Result.ok("已退出登录");
    }

    @ApiOperation("踢人下线")
    @GetMapping("/kickout/{userId}")
    public Result kickout(@PathVariable("userId") Long userId) {
        if (userId == null || userId <= 0) {
            return Result.error("用户id不能为null且必须大于等于1");
        }
        // 本质是删除 指定用户 存储在redis的token信息
        StpUtil.logout(userId);
        return Result.ok("该用户已下线");
    }

    @ApiOperation("账号封禁")
    @PutMapping("/disable")
    public Result disable(@RequestBody @Validated UserDisableVO userDisableVO) {
        return userService.disable(userDisableVO);
    }

    @ApiOperation("账号解封")
    @PutMapping("/untieDisable")
    public Result untieDisable(@RequestBody @Validated UserDisableVO userDisableVO) {
        return userService.untieDisable(userDisableVO);
    }

}
